package com.anolesoft.amljmes.foreknow.service.impl;

import com.anolesoft.amljmes.foreknow.bo.MesBzzPtdmx;
import com.anolesoft.amljmes.foreknow.bo.MesBzzQfbld;
import com.anolesoft.amljmes.foreknow.bo.MesBzzQfbldmx;
import com.anolesoft.amljmes.foreknow.dto.MesBzzPtmlDto;
import com.anolesoft.amljmes.foreknow.dto.MesBzzQfbldDto;
import com.anolesoft.amljmes.foreknow.mapper.MesBzzPtmlMapper;
import com.anolesoft.amljmes.foreknow.mapper.MesBzzQfbldMapper;
import com.anolesoft.amljmes.foreknow.service.MesBzzQfbldService;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtdmxDto;
import com.anolesoft.common.core.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MesBzzQfbldServiceImpl implements MesBzzQfbldService {

    @Autowired
    private MesBzzQfbldMapper mapper;
    @Autowired
    private MesBzzPtmlMapper mesBzzPtmlMapper;
    @Autowired
    private MesBzzQfbldService service;

    @Transactional
    @Override
    public int insertData(List<MesBzzPtdmx> mx) {
        //把需要的字段拿出来放到铅封保留单里
        String a = "QFBL";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNow = sdf.format(date);
        int shui = mapper.liuShuiHao();
        String xulie = shui + "";
        //保留单编号
        String num = a + dateNow + xulie;
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        //当前登录人主键
        String objuid = loginUser.getEmpUid();
        String fcode = loginUser.getFacCode();
        int b = 0;
        String uid = "";
        //加铅封保留单
        for (MesBzzPtdmx qfbldmx: mx) {
            int i = service.selectAll(qfbldmx.getMatchingDocPk());
            //没有存入
            if (i == 0){
                MesBzzQfbld mesBzzQfbld = new MesBzzQfbld();
                mesBzzQfbld.setObjuid(mesBzzQfbld.getObjuid());
                mesBzzQfbld.setBldbh(num);
                mesBzzQfbld.setPtdh(qfbldmx.getPtd().getSupportingOrderNo());
                mesBzzQfbld.setMatchingDocPk(qfbldmx.getMatchingDocPk());
                mesBzzQfbld.setState(0);
                mesBzzQfbld.setCjr(objuid);
                mesBzzQfbld.setCjsj(date);
                mesBzzQfbld.setFactoryCode(fcode);
                mesBzzQfbld.setFathernumber(qfbldmx.getPtd().getFathernumber());
                mesBzzQfbld.setFathername(qfbldmx.getPtd().getFathername());
                mesBzzQfbld.setProportion(qfbldmx.getPtd().getProportion());
                mesBzzQfbld.setDanwei(qfbldmx.getDanwei());
                mesBzzQfbld.setDanjishu(qfbldmx.getDanjishu());
                mesBzzQfbld.setAffiliation(qfbldmx.getPtd().getAffiliation());
                mesBzzQfbld.setDispatch(qfbldmx.getPtd().getDispatch());
                mesBzzQfbld.setBatch(qfbldmx.getPtd().getBatch());
                mesBzzQfbld.setSorties(qfbldmx.getPtd().getSorties());
                b = mapper.insertData(mesBzzQfbld);
                uid = mesBzzQfbld.getObjuid();
            }
            break;
        }
        //加铅封保留单明细
        for (MesBzzPtdmx qfbldmx: mx) {
            int j = service.selectMx(qfbldmx.getObjuid());
            //存入
            if (j == 0){
                qfbldmx.setFactoryCode(fcode);
//                MesBzzPtmlDto ptml = mesBzzPtmlMapper.query(qfbldmx);
//                System.out.println(ptml);
                //拿到铅封保留单id
                if (uid.equals("")){
                    uid = mapper.selectObjuid(qfbldmx.getMatchingDocPk());
                }
                MesBzzQfbldmx mesBzzQfbldmx = new MesBzzQfbldmx();
                mesBzzQfbldmx.setQfbldUid(uid);
                mesBzzQfbldmx.setPtdmxUid(qfbldmx.getObjuid());
                mesBzzQfbldmx.setLeibie(qfbldmx.getPtd().getAffiliation());
                mesBzzQfbldmx.setTuhao(qfbldmx.getPartDrawingNo());
                mesBzzQfbldmx.setMingcheng(qfbldmx.getName());
                mesBzzQfbldmx.setDjs(qfbldmx.getDanjishu());
                mesBzzQfbldmx.setBili(qfbldmx.getPtd().getProportion());
                mesBzzQfbldmx.setPromiseDate(qfbldmx.getQfbldmx().getPromiseDate());
                mesBzzQfbldmx.setBlsl(qfbldmx.getQfbldmx().getBlsl());
                mesBzzQfbldmx.setJinzhan(qfbldmx.getQfbldmx().getJinzhan());
                mesBzzQfbldmx.setJx(qfbldmx.getJixing());
                mesBzzQfbldmx.setJfcj(qfbldmx.getJiaofuchejian());
                mesBzzQfbldmx.setJscj(qfbldmx.getJieshouchejian());
                mesBzzQfbldmx.setJfsx(qfbldmx.getJiaofushunxu());
                mesBzzQfbldmx.setStationName(qfbldmx.getStationName());
                mesBzzQfbldmx.setDrawingNoOfStation(qfbldmx.getDrawingNoOfStation());
                mesBzzQfbldmx.setCzdw(qfbldmx.getJiaofuchejian());
                mesBzzQfbldmx.setPaigonghao(qfbldmx.getPaigonghao());
                mesBzzQfbldmx.setSerialnumber(qfbldmx.getSerialnumber());
                mesBzzQfbldmx.setName(qfbldmx.getName());
                mesBzzQfbldmx.setDivisionroute(qfbldmx.getDivisionroute());
                mesBzzQfbldmx.setDanwei(qfbldmx.getDanwei());
                mesBzzQfbldmx.setDanjishu(qfbldmx.getDanjishu());
                int c = mapper.insertMx(mesBzzQfbldmx);
                //改变配套单明细中保留数量
                //改变状态
                MesBzzPtdmx ptdmx = new MesBzzPtdmx();
                ptdmx.setBaoliunum(qfbldmx.getQfbldmx().getBlsl());
                ptdmx.setObjuid(qfbldmx.getObjuid());

                if (qfbldmx.getQfbldmx().getBlsl() == qfbldmx.getQuantityOfMissingParts()){
                    service.updateStates(ptdmx);
                }else {
                    service.updateState(ptdmx);
                }
            }else {
                //更新
                MesBzzQfbldmx mesBzzQfbldmx = new MesBzzQfbldmx();
                mesBzzQfbldmx.setJinzhan(qfbldmx.getQfbldmx().getJinzhan());
                mesBzzQfbldmx.setPromiseDate(qfbldmx.getQfbldmx().getPromiseDate());
                mesBzzQfbldmx.setBlsl(qfbldmx.getQfbldmx().getBlsl());
                mesBzzQfbldmx.setPtdmxUid(qfbldmx.getObjuid());
                int c = mapper.updateThree(mesBzzQfbldmx);
                //保持同步
                MesBzzPtdmx ptdmx = new MesBzzPtdmx();
                ptdmx.setBaoliunum(qfbldmx.getQfbldmx().getBlsl());
                ptdmx.setObjuid(qfbldmx.getObjuid());
                //改变状态
                if (mesBzzQfbldmx.getBlsl() == qfbldmx.getQuantityOfMissingParts()){
                    service.updateStates(ptdmx);
                }else {
                    service.updateState(ptdmx);
                }
            }
        }
        return b;
    }

    @Override
    public int insertMx(List<MesBzzPtdmx> mx) {
        return 0;
    }

    @Override
    public int updateState(MesBzzPtdmx mesBzzPtdmx) {
        return mapper.updateState(mesBzzPtdmx);
    }

    @Override
    public int updateStates(MesBzzPtdmx mesBzzPtdmx) {
        return mapper.updateStates(mesBzzPtdmx);
    }

    @Override
    public int selectAll(String objuid) {
        return mapper.selectAll(objuid);
    }

    @Override
    public int selectMx(String objuid) {
        return mapper.selectMx(objuid);
    }

    @Override
    public int updateThree(MesBzzQfbldmx qfbldmx) {
        return service.updateThree(qfbldmx);
    }

    @Override
    public List<MesBzzQfbld> query(MesBzzQfbldDto qfbldDto) {
        MesBzzQfbldDto dto = new MesBzzQfbldDto();
        if (qfbldDto.getBldbh() != null){
            dto.setBldbh(qfbldDto.getBldbh().trim());
        }
        dto.setFactoryCode(qfbldDto.getFactoryCode());
        return mapper.query(dto);
    }

    @Override
    public List<MesBzzQfbldmx> queryMx(String objuid) {
        return mapper.queryMx(objuid);
    }

    @Override
    public int updatemxState(String objuid) {
        return mapper.updatemxState(objuid);
    }

    @Override
    public int updateNum(String ptdmxUid) {
        return mapper.updateNum(ptdmxUid);
    }

    @Override
    public int selectNum(String qfbldUid) {
        return mapper.selectNum(qfbldUid);
    }

    @Override
    public int updatebldstate(String qfbldUid) {
        return mapper.updatebldstate(qfbldUid);
    }

    @Override
    public int updateptdstate(String ptdmxUid) {
        return mapper.updateptdstate(ptdmxUid);
    }

    @Override
    public String selectObjuid(String matchingDocPk) {
        return mapper.selectObjuid(matchingDocPk);
    }

    @Override
    public List<MesBzzQfbldmx> querydmingxi(String ids) {
        return mapper.querydmingxi(ids);
    }

    @Override
    public int updateqfbldnum(String objuid) {
        return mapper.updateqfbldnum(objuid);
    }
}
